/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.tools;

/**
 *
 * @author Administrator
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeTool {
	
	public final static String FMT_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss";
	public final static String HHmmssS = "HH:mm:ss";
	public final static String FMT_MMdd = "MM-dd";
	public final static String FMT_yyyyMMdd = "yyyy-MM-dd";
	public final static String FMT_yyyyMM = "yyyy-MM";
	public final static String MY_MM_dd = "MM月dd日";
	public final static String yyyyMMdd = "yyyyMMdd";
	public final static String yyyy = "yyyy";
	private DateTimeTool(){

	}

	
	public static Date   getToday(){
	return CalendarUtil.now(FMT_yyyyMMddHHmmssS);
	}
	public static String formatDateTimetoString(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_yyyyMMddHHmmssS);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoStringyyyy(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(yyyy);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoString0(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(yyyyMMdd);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoString6(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_yyyyMMdd);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoString1(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(HHmmssS);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	
	public static String formatDateTimetoString2(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_MMdd);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoString3(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_yyyyMMdd);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	public static String formatDateTimetoString4(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_yyyyMM);
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	
	public static String formatDateTimetoString5(Date date) {
		String reStr = "";
		if (date == null) {
			return reStr;
		}
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy");
		reStr = sdf.format(date);
		return reStr == null ? "" : reStr;
	}
	/**
	 * 
	 * 
	 * 将今天转化为  XX月XX�?
	 * */
	public static String toDate() {
		String reStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(MY_MM_dd);
		reStr = sdf.format(new Date());
		return reStr == null ? "" : reStr;
	}
	/**
	 * 将字符串转换�? date
	 * */
	public static Date toDate2(String dateSt) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_MMdd);
		try {
			Date date = sdf.parse(dateSt);
			return date;
		} catch (ParseException e) {
			return null;
		}
		
	}
	public static Date toDate3(String dateSt) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy-MM-dd");
		try {
			Date date = sdf.parse(dateSt);
			return date;
		} catch (ParseException e) {
			return null;
		}
		
	}
	
	public static Date toDate4(String dateSt) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(FMT_yyyyMMddHHmmssS);
		try {
			Date date = sdf.parse(dateSt);
			return date;
		} catch (ParseException e) {
			return null;
		}
		
	}
	
	
	
	public static Date toDate5(String dateSt) {
//		if(!StringTool.dataString(dateSt)) return null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(yyyyMMdd);
		try {
			Date date = sdf.parse(dateSt);
			return date;
		} catch (ParseException e) {
			return null;
		}
		
	}
	
	public static long getMillisecondsOfTwoDate(Date d1,Date d2) throws Exception {
		if(d1==null || d2==null){
			 throw new NullPointerException();
		}
		long dI1 = d1.getTime();
		long dI2 = d2.getTime();
		return (dI1-dI2);
	}

	
	public static long mTos(String m) throws Exception {
		if(m==null) {
			return 0;
		}

		int min=Integer.parseInt(m);
		return 60*1000*min;
	}
	public static Date stringToDate(String st,String format){
		SimpleDateFormat sdf = new SimpleDateFormat();
		try {
			sdf.applyPattern(format);
			Date a = sdf.parse(st);
			return a;
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Long getDiffDay(String beginDate,String endDate) {
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 Long checkday=0l; 
		 try {
		checkday = (formatter.parse(endDate).getTime()- formatter.parse(beginDate).getTime())/(1000*24*60*60);
		} catch (ParseException e) {
		e.printStackTrace();
		checkday=null;
		}
		 return checkday;
		 }  
	/**
	 * 比较两个时间前后顺序
	 * @param date1
	 * @param date2
	 * @return
	 */
    public static boolean compare_date(Date date1, Date date2) {
        try {
            if (date1.before(date2)) {
                return true;
            }else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
     }
    
    /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate)    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));  
        } catch (ParseException e) {
			e.printStackTrace();
		}  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }    
	
    /**
     * 返回日期是否在范围内
     * @param startDate
     * @param endDate
     * @param date
     * @return
     */
    public static boolean compareDate(Date startDate,Date endDate,Date date){
   	 boolean flag_start=true;
   	 boolean flag_end=true;
   	if(startDate!=null){
   		flag_start=DateTimeTool.daysBetween(startDate,date)>=0;
   	}
   	if(endDate!=null){
   		flag_end=DateTimeTool.daysBetween(date,endDate)>=0;
   	}
   	return  flag_start&&flag_end;
   }
    
	public static void main(String[] args) {
//		System.out.println(CalendarUtil.getTomorrow());
//		System.out.println(CalendarUtil.now("yyyy-MM-dd"));
//		System.out.println(CalendarUtil.getMinusDay(CalendarUtil.now("yyyy-MM-dd"),CalendarUtil.now("yyyy-MM-dd")));
		//Date dangqianDate = new Date();
//		long xiangcT = getDiffDay("2014-02-02",formatDateTimetoString(dangqianDate));
//		long zhoumo = xiangcT%7; //周几  0代表周末
//		int dd = Integer.parseInt(formatDateTimetoString3(dangqianDate).substring(formatDateTimetoString3(dangqianDate).lastIndexOf("-")+1,formatDateTimetoString3(dangqianDate).length()));
//		int jiaoT = 0;
//		
//		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
//
//		int hour = c.get(Calendar.HOUR_OF_DAY); 
//		int minute = c.get(Calendar.MINUTE); 
//System.out.println(formatDateTimetoStringyyyy(new Date()));
		
//		System.out.println("==> "+hour+" =>"+minute);
//		if(dd>=24){//高峰�?
////			if()
//			
//			
//		} else { //非高峰期
//			
//		}
	}
}
